/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrs.util;

/**
 *
 * @author Pablo
 */
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import mrs.models.Recipe;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RecipeParser {

    public static Recipe parse(String fileName) {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file

            Element docEle = db.parse(fileName).getDocumentElement();

            //get a nodelist of elements
            NodeList nl = docEle.getElementsByTagName("head");
            Element headElem = (Element) nl.item(0);
            String title = getTextValue(headElem, "title");
            String servings = getTextValue(headElem, "yield");


            nl = docEle.getElementsByTagName("directions");
            String directions = new String();

    		if(nl != null && nl.getLength() > 0) {
    			for(int i = 0 ; i < nl.getLength();i++) {
    				headElem = (Element)nl.item(i);
                    directions += getTextValue(headElem, "step");
    			}
    		}

            System.out.println(title);
            System.out.println(servings);
            System.out.println(directions);

            return new Recipe(title, servings, directions);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
            return null;
        } catch (SAXException se) {
            se.printStackTrace();
            return null;
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return null;
        }
    }

    private static Document parseRecipeMLFile(String fileName) {
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file

            Element docEle = db.parse(fileName).getDocumentElement();

            //get a nodelist of elements
            NodeList nl = docEle.getElementsByTagName("head");
            Element headElem = (Element) nl.item(1);
            String title = getTextValue(headElem, "title");
            String servings = getTextValue(headElem, "yield");

            System.out.println(title + " " + servings);

            return db.parse(fileName);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }

    private static void parseDocument(Document recipeDoc) {
        //get the root elememt
        Element docEle = recipeDoc.getDocumentElement();

        //get a nodelist of elements
        NodeList nl = docEle.getElementsByTagName("head");
        Element headElem = (Element) nl.item(1);
        String title = getTextValue(headElem, "title");
        String servings = getTextValue(headElem, "yield");

        System.out.println(title + " " + servings);

//		if(nl != null && nl.getLength() > 0) {
//			for(int i = 0 ; i < nl.getLength();i++) {
//
//				//get the recipe element
//				Element el = (Element)nl.item(i);
//
//				//get the Recipe object
//				Recipe recipe = getRecipe(el);
//
//			}
//		}

    }

    private static Recipe getRecipe(Element recipeElement) {

        //for each <recipe> element get text or int values of
        //title, yield, directions
        String title = getTextValue(recipeElement, "title");
        String servings = getTextValue(recipeElement, "yield");

        //Create a new Recipe with the value read from the xml nodes

        System.out.println(title + " " + servings);

        return new Recipe(title, servings);
    }

    /**
     * I take a xml element and the tag name, look for the tag and get
     * the text content
     * i.e for <employee><name>John</name></employee> xml snippet if
     * the Element points to employee node and tagName is name I will return John
     * @param ele
     * @param tagName
     * @return
     */
    private static String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            textVal = el.getFirstChild().getNodeValue();
        }

        return textVal;
    }

}
